import * as actionTypes from './constants'

import {
  getTopBanners,
  getHotRecommends,
  getNewAlbums,
  getRecommendToplists,
  getArtistList
} from '@/services/recommend'

const changeBannerAction = (res) => ({
  type: actionTypes.CHANGE_TOP_BANNERS,
  topBanners: res.banners
})

export const getTopBannerAction = () => {
  return dispatch => {
    getTopBanners().then(res => {
      dispatch(changeBannerAction(res))
    })
  }
}

const changeHotRecommendAction = (res) => ({
  type: actionTypes.CHANGE_HOT_RECOMMENDS,
  hotRecommends: res.result
})

export const getHotRecommendAction = (limit) => {
  return dispatch => {
    getHotRecommends(limit).then(res => {
      dispatch(changeHotRecommendAction(res))
    })
  }
}

const changeNewAlbumAction = (res) => ({
  type: actionTypes.CHANGE_NEW_ALBUMS,
  newAlbums: res.albums
})

export const getNewAlbumAction = (limit) => {
  return dispatch => {
    getNewAlbums(limit).then(res => {
      dispatch(changeNewAlbumAction(res))
    })
  }
}

const changeUplistAction = (res) => ({
  type: actionTypes.CHANGE_UP_LIST,
  uplist: res.playlist
})

const changeNewlistAction = (res) => ({
  type: actionTypes.CHANGE_NEW_LIST,
  newlist: res.playlist
})

const changePrevlistAction = (res) => ({
  type: actionTypes.CHANGE_PREV_LIST,
  prevlist: res.playlist
})

export const getRecommendToplistAction = (idx) => {
  return dispatch => {
    getRecommendToplists(idx).then(res => {
      switch (idx) {
        case 0:
          dispatch(changeUplistAction(res))
          break
        case 2:
          dispatch(changeNewlistAction(res))
          break
        case 3:
          dispatch(changePrevlistAction(res))
          break
        default:
      }
    })
  }
}

// 入驻歌手
const changeArtistListAction = (res) => ({
  type: actionTypes.CHANGE_ARTIST_LIST,
  artistList: res.artists
})

export const getArtistListAction = () => {
  return dispatch => {
    getArtistList(5, 5001).then(res => {
      dispatch(changeArtistListAction(res))
    })
  }
}